//
//  AppDelegate.swift
//  sinaBlog
//
//  Created by jobo on 15/10/27.
//  Copyright © 2015年 jobo. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

//dafdas
    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        //let tabBarController = JBMainViewController()
        //let welcomeVC = JBWelcomeViewController()
        //let newFeatureVC = JBNewFeatureCollectionViewController()

        //window?.rootViewController = tabBarController
        //window?.rootViewController = welcomeVC
        //window?.rootViewController = newFeatureVC
        
        window?.rootViewController = defaultController()
        
        window?.makeKeyAndVisible()
        
        
        return true
    }

    
    private func defaultController() -> UIViewController {
        // 判断是否登录
        // 每次判断都需要 == nil
        if JBUserAccount.loadAccount() == nil
        {
            return JBMainViewController()
        }
        
        // 判断是否是新版本
        return isNewVersion() ? JBNewFeatureCollectionViewController  () : JBWelcomeViewController()
    }

    /// 判断是否是新版本
    private func isNewVersion() -> Bool {
        // 获取当前的版本号
        let versionString = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let currentVersion = Double(versionString)!
        print("currentVersion: \(currentVersion)")
        
        // 获取到之前的版本号
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        print("sandboxVersion: \(sandboxVersion)")
        
        // 保存当前版本号
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        
        // 对比
        return currentVersion > sandboxVersion
    }
    
    // 切换跟控制器
    func switchController(isMain : Bool)
    {
        window?.rootViewController = isMain ? JBMainViewController() : JBWelcomeViewController()
    }

}

